#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.db import connection

from web.dao.base_dao import BaseDao
from web.models.optimize2_account import Optimize2Account

class Optimize2AccountDao(BaseDao):
    """
    Optimize2Account的dao类
    """

    model_class = Optimize2Account

    def find_all(self):
        """
        查询所有记录
        """

        return self.find_list(dict(), dict(), dict())

    def find_by_account_name(self, account_name:str) -> Optimize2Account:
        """
        根据账户名称查询记录
        """

        filter_dict = {'account_name': account_name}
        return self.find_one(filter_dict, dict(), list())

    def update_after_close_position(self, sell_or_buy_commodity_future_number: int, hold_commodity_future_assets: float,
                                    close_position_commodity_future_assets: float, capital_assets: float, account_name: str):
        """
        平仓之后更新quant2_account
        """

        with connection.cursor() as cursor:
            sql: str = "update optimize2_account t " \
                       "set t.hold_commodity_future_number = {}, " \
                       "t.commodity_future_assets = round({}, 4), " \
                       "t.capital_assets = round(t.capital_assets + {}, 4), " \
                       "t.total_assets = round({} + {} + {}, 4), " \
                       "t.total_open_commission = (select COALESCE(sum(t1.open_commission), 0) from optimize2_c_f_transact_record t1 where t1.account_name = '{}'), " \
                       "t.total_close_commission = (select COALESCE(sum(t1.close_commission), 0) from optimize2_c_f_transact_record t1 where t1.account_name = '{}') " \
                       "where t.account_name = '{}'".format(sell_or_buy_commodity_future_number,
                                                            hold_commodity_future_assets,
                                                            close_position_commodity_future_assets,
                                                            hold_commodity_future_assets, capital_assets, close_position_commodity_future_assets,
                                                            account_name,
                                                            account_name,
                                                            account_name)

            cursor.execute(sql)

    def update_after_open_position(self, sell_or_buy_commodity_future_number: int, commodity_future_assets: float, current_open_commodity_future_assets: float,
                                   total_open_position_commission: float, current_open_position_commission: float, account_name: str):
        """
        开仓之后更新optimize2_account。
        capital_assets = capital_assets - 新开仓的期货成本 - 新开仓的期货费用
        total_assets = 全部期货资产（包括已经持仓的和新开仓的） + capital_assets - 新开仓的期货成本 - 新开仓的期货费用
        """

        with connection.cursor() as cursor:
            sql: str = "update optimize2_account t " \
                        "set t.hold_commodity_future_number = {}, " \
                        "t.commodity_future_assets = round({}, 4), " \
                        "t.total_assets = round({} + t.capital_assets - {} - {}, 4), " \
                        "t.capital_assets = round(t.capital_assets - {} - {}, 4), " \
                        "t.total_open_commission = round({}, 4) " \
                        "where t.account_name = '{}'".format(sell_or_buy_commodity_future_number,
                                                 commodity_future_assets,
                                                 commodity_future_assets, current_open_commodity_future_assets, current_open_position_commission,
                                                 current_open_commodity_future_assets, current_open_position_commission,
                                                 total_open_position_commission,
                                                 account_name)
            cursor.execute(sql)